package com.czk.plugins;


import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.scripting.defaults.DefaultParameterHandler;
import org.springframework.stereotype.Component;

import java.sql.PreparedStatement;

/**
 * 参数替换插件
 *
 * @Author:ChenZhangKun
 * @Date: 2021/4/19 15:14
 */
@Component
// 指定拦截的类和方法
@Intercepts(@Signature(type = ParameterHandler.class, method = "setParameters", args = PreparedStatement.class))
public class ParameterPlugin implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        // 拿到被代理的对象
        DefaultParameterHandler target = (DefaultParameterHandler) invocation.getTarget();
        // 拿到参数
        Object parameterObject = target.getParameterObject();

        Object proceed = invocation.proceed();
        return proceed;
    }
}
